package com.example.mq.mqserver.datacenter;

import com.example.mq.MqApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

/**
 * 用这个类来管理数据库上的数据：Exchange，Binding，MSGQueue
 */
@Slf4j
public class DataBaseManage {
    private MetaMapper metaMapper;
    //创建数据库和表
    public void init() {
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        //判断数据库是否存在
        if(!isExistsDataBase()) {
            File file = new File("./data");
            boolean ret = file.mkdirs();
            if(ret) {
                log.info("创建数据库成功");
            }else {
                log.info("创建数据库失败");
            }

            //创建表
            createTable();

            //添加默认数据
            createDefaultData();

            log.info("数据库初始化已完成");
        }else {
            log.info("数据库已存在");
        }

    }

    public void deleteDB() {
        //删除数据库中的文件
        File file = new File("./data/meta.db");
        log.info(file.getAbsolutePath());
        boolean ret = file.delete();
        if (ret) {
            log.info("删除数据库中文件成功");
        }else {
            log.info("删除数据库中文件失败");
        }

        //删除目录
        file = new File("./data");
        log.info(file.getAbsolutePath());
        ret = file.delete();
        if(ret) {
            log.info("删除目录成功");
        }else {
            log.info("删除目录失败");
        }
    }

    //此处主要是添加一个默认的交换机
    //构造一个DIRECT类型的交换机
    //在RabbitMQ中有一个设定：带有一个匿名的交换机，类型是DIRECT
    private void createDefaultData() {
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        log.info("插入exchange成功");
    }

    private void createTable() {
        metaMapper.createTableQueue();
        metaMapper.createTableBinding();
        metaMapper.createTableExchange();
        log.info("[createTable] 创建表完成");
    }

    private boolean isExistsDataBase() {
        File file = new File("./data/meta.db");
        return file.exists();
    }

    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public List<Exchange> selectExchanges() {
        return metaMapper.selectExchanges();
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }
    public List<Binding> selectBindings() {
        return metaMapper.selectBindings();
    }

    public void insertQueue(MSGQueue msgQueue) {
        metaMapper.insertQueue(msgQueue);
    }
    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }
    public List<MSGQueue> selectQueues() {
        return metaMapper.selectQueues();
    }
}
